<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>博客正文</title>
    <link rel="stylesheet" href="css/conmmon.css">
    <link rel="stylesheet" href="css/blog_content.css">
    <link rel="stylesheet" href="editor.md/css/editormd.preview.min.css" />
    <script src="js/jquery.min.js"></script>
    <script src="editor.md/editormd.js"></script>
    <script src="editor.md/lib/marked.min.js"></script>
    <script src="editor.md/lib/prettify.min.js"></script>
</head>

<body>
    <!-- 导航栏 -->
    <div class="nav">
        <img src="img/logo2.jpg" alt="">
        <span class="title">我的博客系统</span>
        <!-- 用来占据中间位置 -->
        <span class="spacer"></span>
        <a href="blog_list.html">主页</a>
        <a href="blog_edit.html">写博客</a>
        <a href="javascript:onExit()">退出登录</a>
    </div>
    <!-- 版心 -->
    <div class="container">
        <!-- 左侧个人信息 -->
        <div class="container-left">
            <div class="card">
                <img src="img/doge.jpg" class="avtar" alt="">
                <h3>Mattylyh</h3>
                <a href="http:www.gitee.com">Gitee</a>
                <div class="counter">
                    <span>文章</span>
                    <span>分类</span>
                </div>
                <div class="counter">
                    <span>2</span>
                    <span>1</span>
                </div>
            </div>
        </div>
        <!-- 右侧内容详情 -->
        <div class="container-right">
            <div class="blog-content">
                <!-- 博客标题 -->
                <h3 id="title"></h3>
                <!-- 博客时间 -->
                <div id="data" class="date">
                    编辑日期： <span id="date"></span>
                    <br></br>
                    浏览量： <span id="rcount"></span>&nbsp;&nbsp;&nbsp;&nbsp;
                </div>
                <!-- 博客正文 --> 
                <div id="editorDiv"></div>
            </div>
        </div>
    </div>
    <script type="text/javascript">
            var editormd;
            function initEdit(md){
                editormd = editormd.markdownToHTML("editorDiv", {
                markdown : md, // Also, you can dynamic set Markdown text
                // htmlDecode : true,  // Enable / disable HTML tag encode.
                // htmlDecode : "style,script,iframe",  // Note: If enabled, you should filter some dangerous HTML tags for website security.
                });
            }

            // 获取URL中queryString参数
            function getURLParam(key){
                // 通过location.search拿到URL
                var params = location.search;
                // 有query string就进入下一步
                if(params.indexOf("?")>=0){
                //    获得query string
                params = params.substring(params.indexOf("?") + 1);
                    //获得键值对集合 
                var paramArr = params.split('&');
                    //遍历键值对集合 
                for(var i=0;i<paramArr.length;i++){
                        // 分割键和值
                        var namevalues = paramArr[i].split("=");
                        // key是函数的参数 和参数匹配就返回对应的value
                        if (namevalues[0]==key) {
                            return namevalues[1];
                        }
                    }
                } else {
                    return "";
                }
            }
            
            function getDetail() {
                var aid = getURLParam("id");
                if (aid != null && aid > 0) {
                    jQuery.ajax({
                        url:"/art/detail",
                        type:"GET",
                        data:{
                            "aid": aid
                        },
                        success: function(body) {
                            if (body.code == 200 && body.data != null) {
                                var art = body.data;
                                jQuery("#title").text(art.title);
                                jQuery("#data").text(art.createtime);
                                jQuery("#rcount").text(art.rcount);
                                editormd = editormd.markdownToHTML("editorDiv", {
                                    markdown: art.content
                                });
                            }
                        },
                        error: function(body) {}
                    })
                }
            }
            getDetail();
            
    </script> 
</body>
<script>
    function onExit() {
        // confirm弹出一个选择框 选择确认就会返回true
        if (confirm("真的要退出么？")) {
            jQuery.ajax({
                url:"/user/logout",
                type:"POST",
                data:{},
                success: function(body) {
                    location.href = "/login.html";
                },
                error: function(body) {
                    if (body != null && body.status == 401) {
                        alert("未登录");
                    }
                    alert(JSON.stringify(body)); 
                }
            }) 
        }
    }
</script>
</html>